part of 'page.dart';

class _AppearanceSettingsPageWloc
    extends StatefulWloc<_AppearanceSettingsPageWlocState, AppearanceSettingsPage> {
  late final bloc.AppearanceSettingsPageBloc _bloc;

  @override
  _AppearanceSettingsPageWlocState get initialState {
    return _AppearanceSettingsPageWlocStateFactory().create(
      blocState: _bloc.state,
    );
  }

  bool isLightThemePrimaryColorAvailable({
    required LightThemePrimaryColor lightThemePrimaryColor,
  }) {
    return state.primaryColorChangePermitted ||
        state.lightThemeEffectivePrimaryColor == lightThemePrimaryColor;
  }

  bool isDarkThemePrimaryColorAvailable({
    required DarkThemePrimaryColor darkThemePrimaryColor,
  }) {
    return state.primaryColorChangePermitted ||
        state.darkThemeEffectivePrimaryColor == darkThemePrimaryColor;
  }

  void onThemeModeTilePressed({
    required ThemeMode themeMode,
  }) {
    _bloc.requestThemeModeChange(
      themeMode: themeMode,
    );
  }

  void onLightThemePrimaryColorTilePressed({
    required LightThemePrimaryColor lightThemePrimaryColor,
  }) {
    _bloc.requestLightThemeSelectedPrimaryColorChange(
      selectedPrimaryColor: lightThemePrimaryColor,
    );
  }

  void onDarkThemePrimaryColorTilePressed({
    required DarkThemePrimaryColor darkThemePrimaryColor,
  }) {
    _bloc.requestDarkThemeSelectedPrimaryColorChange(
      selectedPrimaryColor: darkThemePrimaryColor,
    );
  }

  void _onBlocStateChanged(bloc.AppearanceSettingsPageBlocState blocState) {
    final updatedState = _AppearanceSettingsPageWlocStateFactory().create(
      blocState: blocState,
    );

    emit(updatedState);
  }

  @override
  void initState() {
    super.initState();

    _bloc = bloc.AppearanceSettingsPageBloc(
      settingsBloc: core_di.sl(),
      userInfoBloc: core_di.sl(),
      purchaseFlowDisplayBloc: core_di.sl(),
      changeDarkThemeSelectedPrimaryColor: appearance_settings_page_di.sl(),
      changeLightThemeSelectedPrimaryColor: appearance_settings_page_di.sl(),
      changeThemeMode: appearance_settings_page_di.sl(),
      handlePremiumAccessStatusOuterUpdate: appearance_settings_page_di.sl(),
    );

    _bloc.stream.listen(_onBlocStateChanged);
  }

  @override
  void dispose() {
    _bloc.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _AppearanceSettingsPageView(
      wloc: this,
    );
  }
}
